<?php

namespace app\admin\controller\yalimedia;

use addons\yalimedia\library\AlibabaCloundHelper;
use AlibabaCloud\Client\Exception\ServerException;
use app\common\controller\Backend;

/**
 * 课程分类
 *
 * @icon fa fa-circle-o
 */
class Category extends Backend
{


    protected $noNeedRight = [];
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try {
                    $cateName = $params['catename'];
                    $cateId = $params['cate_id'];
                    $cateId = AlibabaCloundHelper::instance()->add_category($cateName,$cateId);
                    if($cateId){
                        $this->success("请求成功");
                    }

                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $type = $this->request->get('type');
        $categoryModel = new \app\admin\model\yalimedia\Category();
        if(!$category = $categoryModel->where(['type'=>$type])->find()){
            exit;
        }else{
            $cateId = $category['cateid'];
        }
        
        //查看当前是否已经生成根分类
        $list = AlibabaCloundHelper::instance()->categories($cateId);
        $chanelList[$cateId] = ['id'=>$cateId,'text'=>"顶级分类"];
        foreach($list as $k=>$v){
            $chanelList[$v->CateId] = ['id'=>$v->CateId,'text'=>$v->CateName];
        }

        $this->assign('category',$chanelList);

        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {

        $row = (array)AlibabaCloundHelper::instance()->categorie($ids);

        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    $cateName = $params['catename'];
                    $cateId = AlibabaCloundHelper::instance()->edit_category($ids,$cateName);
                    if($cateId){
                        $this->success("请求成功");
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                } catch (ServerException $e){
                    $this->error("编辑失败，请检查阿里云服务");
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            try {
                if(AlibabaCloundHelper::instance()->del_category($ids)){
                    $this->success('请求成功');
                }else{
                    $this->error('删除失败');
                }
            }catch (ServerException $e){
                $this->error("删除失败，请检查阿里云服务");
            }


        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
    

}
